package com.core.zyyz.pcmc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.core.common.utils.SecurityUtils;
import com.core.common.utils.poi.ExcelUtil;
import com.core.zyyz.pcmc.domain.PcmcRole;
import com.core.zyyz.pcmc.domain.PcmcRoleUser;
import com.core.zyyz.pcmc.domain.PcmcUser;
import com.core.zyyz.pcmc.service.PcmcUserService;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户Controller
 *
 * @author core
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/pcmc/user")
public class PcmcUserController extends BaseController {

    private static final String PASSWORD = "888888";//初始密码

    @Autowired
    private PcmcUserService pcmcUserService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(PcmcUser pcmcUser) {
        startPage();
        List<PcmcUser> list = pcmcUserService.selectPcmcUserList(pcmcUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcmcUser pcmcUser) {
        List<PcmcUser> list = pcmcUserService.selectPcmcUserList(pcmcUser);
        ExcelUtil<PcmcUser> util = new ExcelUtil<PcmcUser>(PcmcUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('pcmc:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<PcmcUser> util = new ExcelUtil<PcmcUser>(PcmcUser.class);
        List<PcmcUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = pcmcUserService.importUser(userList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<PcmcUser> util = new ExcelUtil<PcmcUser>(PcmcUser.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:query')")
    @GetMapping(value = "/{userCode}")
    public AjaxResult getInfo(@PathVariable("userCode") String userCode)
    {
        return success(pcmcUserService.selectPcmcUserByUserCode(userCode));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/insertUser")
    public AjaxResult add(@RequestBody PcmcUser pcmcUser) throws Exception {
        pcmcUser.setUserPassword(SecurityUtils.encryptPassword(PASSWORD)); //设置默认密码
        return toAjax(pcmcUserService.insertPcmcUser(pcmcUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/updateUser")
    public AjaxResult edit(@RequestBody PcmcUser pcmcUser) {
        return toAjax(pcmcUserService.updatePcmcUser(pcmcUser));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping("/{userCodes}")
    public AjaxResult remove(@PathVariable String[] userCodes) {
        int i = pcmcUserService.deletePcmcUserByUserCodes(userCodes);
        return toAjax(i);
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody PcmcUser user) {
        user.setUserPassword(SecurityUtils.encryptPassword(PASSWORD));
        return toAjax(pcmcUserService.resetPwd(user));
    }

    /**
     * 查询用户未授权的角色
     */
    @GetMapping({"/queryUserUnGrantRoleList"})
    public TableDataInfo queryUserUnGrantRoleList() {
//        startPage();
        List<PcmcRole> list = this.pcmcUserService.queryUserUnGrantRoleList();
        return getDataTable(list);
    }

    /**
     * 查询用户已授权的角色
     */
    @PostMapping({"/queryUserGrantRoleList"})
    public TableDataInfo queryUserGrantRoleList(@RequestBody PcmcRole param) {
//        startPage();
        List<PcmcRole> list = this.pcmcUserService.queryUserGrantRoleList(param);
        return getDataTable(list);
    }

    /**
     * 用户授权角色
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PostMapping("/authRole")
    public AjaxResult insertAuthRole(@RequestBody PcmcRoleUser pcmcRoleUser) {
        pcmcUserService.insertUserAuth(pcmcRoleUser);
        return success();
    }
}
